<template>
  <div>
    <el-button type="primary" @click="openDialog()">新增老人</el-button>
    <el-table :data="list" style="width: 100%">
      <el-table-column prop="name" label="姓名" width="120" />
      <el-table-column prop="gender" label="性别" width="80" />
      <el-table-column prop="birthday" label="生日" width="120" />
      <el-table-column prop="contact" label="联系方式" width="150" />
      <el-table-column label="操作" width="150">
        <template #default="scope">
          <el-button size="mini" @click="edit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="remove(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="老人信息" :visible.sync="dialogVisible">
      <el-form ref="form" :model="form">
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth">
          <el-select v-model="form.gender" placeholder="请选择">
            <el-option label="男" value="男" />
            <el-option label="女" value="女" />
          </el-select>
        </el-form-item>
        <el-form-item label="生日" :label-width="formLabelWidth">
          <el-date-picker v-model="form.birthday" type="date" placeholder="选择日期" />
        </el-form-item>
        <el-form-item label="联系方式" :label-width="formLabelWidth">
          <el-input v-model="form.contact" />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listElder, addElder, updateElder, delElder } from '@/api/elder'

export default {
  data() {
    return {
      list: [],
      dialogVisible: false,
      form: {},
      formLabelWidth: '80px',
      isEdit: false
    }
  },
  mounted() {
    this.loadData()
  },
  methods: {
    loadData() {
      listElder().then(res => {
        if (res.code === 200) {
          this.list = res.data
        }
      })
    },
    openDialog() {
      this.form = {}
      this.isEdit = false
      this.dialogVisible = true
    },
    edit(row) {
      this.form = Object.assign({}, row)
      this.isEdit = true
      this.dialogVisible = true
    },
    save() {
      if (this.isEdit) {
        updateElder(this.form).then(res => {
          this.handleResponse(res)
        })
      } else {
        addElder(this.form).then(res => {
          this.handleResponse(res)
        })
      }
    },
    remove(id) {
      this.$confirm('确认删除该老人？', '提示', {
        type: 'warning'
      })
        .then(() => {
          delElder(id).then(res => {
            this.handleResponse(res)
          })
        })
        .catch(() => {})
    },
    handleResponse(res) {
      if (res.code === 200) {
        this.$message.success('操作成功')
        this.dialogVisible = false
        this.loadData()
      } else {
        this.$message.error(res.msg)
      }
    }
  }
}
</script>

<style scoped lang="scss">

</style>
